package com.katsu.myapplib.app;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;

import androidx.appcompat.app.AppCompatActivity;

import com.katsu.myapplib.activity.ActivityStack;
import com.katsu.myapplib.http.Http;
import com.katsu.myapplib.sign.f;
import com.katsu.myapplib.sign.z;
import com.katsu.myapplib.sign.g;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import timber.log.Timber;

@f
@z
@AppInit()
public class AppApplication extends Application {
    public static AppApplication INSTANCE;

    public static boolean e;

    public static int k;


    private final Map<Class<?>, AppCallBack> b = new HashMap<>();

    //	private final Stack<Activity> a = new Stack<>();

    private AppInit i;

    @Override
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    public void onCreate() {

        INSTANCE = this;

        ActivityStack.init();


     if (g.a(this)) {
        } else {
            appExit();
        }
        i();
        super.onCreate();

    }


    public void finishActivity(AppCompatActivity activity) {

        try {

            ActivityStack.finishActivity(activity);

        } catch (Exception e) {
        }

    }

    public void finishActivity() {


        ActivityStack.finishActivity(ActivityStack.getTopActivity());
    }


    public void retainActivity(Class<? extends AppCompatActivity>... clss) {

        //		Stack<Activity> s = (Stack<Activity>) a.clone();

        Stack<AppCompatActivity> s = (Stack<AppCompatActivity>) ActivityStack.getActivityStack().clone();


        for (AppCompatActivity a : s) {

            boolean k = true;

            for (Class<?> as : clss) {

                if (a.getClass().equals(as)) {

                    k = false;

                    continue;

                }

            }

            if (k) {

                finishActivity(a);

            }

        }

    }

    public boolean hasActivity(Class<? extends AppCompatActivity> cls) {

        for (int i = 0; i < ActivityStack.getActivityStack().size(); i++) {

            if (ActivityStack.getActivityStack().get(i).getClass().equals(cls)) {

                return true;

            }

        }

        return false;

    }


    public void appExit() {

        try {
            ActivityStack.finishAllActivity();
            ActivityStack.release();
            ActivityManager e = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            e.restartPackage(getPackageName());
            android.os.Process.killProcess(Process.myPid());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    private void i() {

        try {

            Timber.plant(new Timber.DebugTree());

        } catch (Exception e) {
        }

        try {

            Http.getInstance().setIsLog(i.log());


        } catch (Exception e) {
        }

        if (i.crash()) {

            new Handler(Looper.getMainLooper()).post(new Runnable() {

                @Override
                public void run() {

                    while (true) {

                        try {

                            Looper.loop();

                        } catch (Throwable e) {
                        }

                    }

                }

            });

        }

    }




    public interface OnAppRootCallBack {

        void onSuccess();

    }
}
